﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StorageApp.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Effects;
using System.Xml.Linq;
using ZXing.QrCode.Internal;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace StorageApp.APIS
{
    class api
    {  
        static readonly string apiUrl = "";

        public static void writeLog(string mes)
        {
            if (Method.IniReadValue("log", "islog") == "true")
            {
                string filePath =   Method.IniReadValue("log", "logpath");
                using (FileStream stream = new FileStream(filePath, FileMode.Append))
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine($"{DateTime.Now}," + mes);
                }
            }
        }

        public static async Task<JObject> api_employee(string process_id, string shift_id = "")
        {
            string cmd = "/pro/processes/user?process_id=" + process_id ;
            if (shift_id != "")
            {
                cmd += "&shift_id=" + shift_id;
            }
            return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
        }
          public static async Task<JObject> api_device()
            {
                string cmd = "/baseData/loom/list";
           
                return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
            }
        public static async Task<JObject> api_class(string process_id)
        {
            string cmd = "/pro/processes/userclass?process_id=" + process_id;
     
            return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
        }
        //获取检验库存
        public static async Task<JObject> scrapStatus(string page,string pageSize)
        {
            string cmd = "/baseData/defect/list?page="+ page+ "&pageSize="+ pageSize; 
            return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
        }

        public static async Task<JObject> getWip(string filter = "",string page= "1", string pageSize="40")
        {
            string cmd = "/pro/wip/list?page="+ page+ "&pageSize="+ pageSize;
            if (filter != "")
            { 
                cmd = cmd + filter;
            }
            return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
        }
        public static async Task<JObject> deletePassByWip(object storage)
        {
            string cmd = "/pro/processes/deletePassByWip";

            var param = new Dictionary<string, string>
                        { 
                          {"details",JsonConvert.SerializeObject(storage, Formatting.Indented)}, 
                        };
            return await HttpWebUtility.CreatePostHttpResponse(apiUrl + cmd, param);
        }
        public static async Task<JObject> getOpenCode(string? barcode = null)
        {
            string cmd = "/pro/OpenCard/detailinfo";
            if (barcode != null)
            {
                cmd = cmd + "?filter[0][code][type]=eq&filter[0][code][value]=" + barcode;
            }
            return await HttpWebUtility.CreateGetHttpResponse(apiUrl + cmd);
        }
        public static async Task<JObject> flowAdd(string? Operator_id, object storage,string quantity, string scrap_quantity, string defect, string speed,string temperature,string ?Device_id,string remark)
        {
            string cmd = "/pro/processes/swipeone";

            var param = new Dictionary<string, string>
                        {
                            { "swipor_id", Operator_id??"" }, 
                            {"details",JsonConvert.SerializeObject(storage, Formatting.Indented)},
                            {"pro_dept_id",Method.IniReadValue("pro_dept_id") },
                            {"quantity",quantity},
                            {"scrap_quantity",scrap_quantity},
                            { "defects", defect},
                            { "speed", speed},
                            { "temperature", temperature },
                            { "device_id", Device_id??"0" },
                            { "remark", remark },
                        };

            return await HttpWebUtility.CreatePostHttpResponse(apiUrl + cmd, param);
        } 
        public static async Task<JObject> qpi_list(string fk_)
        {
            string cmd = "pro/processes/pass_list?fk_=" + fk_;
            var param = new Dictionary<string, string>
                        {
                            { "swipor_id", "" },
            };
            return await HttpWebUtility.CreatePostHttpResponse(apiUrl + cmd, param);
        } 
      
    }
}
